//
//  JCEmotionPageCell.swift
//  SinaWeibo_18
//
//  Created by mac on 15/11/4.
//  Copyright © 2015年 cz. All rights reserved.
//

import UIKit

class JCEmotionPageCell: UICollectionViewCell {
 
//    var label: UILabel?
//    var labelText: String? {
//        didSet{
//            label?.text = labelText
//        }
//    }
    //保存表情按钮的集合
    private lazy var emotionButtons: [UIButton] = [UIButton]()
    
    //当前页显示的表情数据
    var emotions: [JCEmotion]? {
        didSet{
            
            //隐藏所有的按钮
            for value in emotionButtons {
                value.hidden = true
            }
            
            
            //遍历每一个按钮,设置表情数据
            for(index, value) in emotions!.enumerate(){
                let button = emotionButtons[index]
                //若有显示
                button.hidden =  false
                
                //判断是否是图片表情或emoji
                if value.type == 0 {
                    let name = "\(value.prepath!)/\(value.png!)"
                    button.setImage(UIImage(named: name), forState: UIControlState.Normal)
                    //当前是图片表情  设置文字为nil
                    button.setTitle(nil, forState: UIControlState.Normal)
                }else{
                    //当前是图片表情 设置图片为nil
                    button.setImage(nil, forState: UIControlState.Normal)
                    //emoji表情
                    button.setTitle((value.code! as NSString).emoji(), forState: UIControlState.Normal)
                }
            }
        }
    }
    
    var indexPath: NSIndexPath? {
        didSet{
            //只在最近表情中显示
            recentLabel.hidden = indexPath?.row != 0
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        backgroundColor = randomColor()
        
        

        //添加最近label
        contentView.addSubview(recentLabel)
        
        //添加20个表情按钮
        for _ in 0..<PAGE_EMOTION_TOTAL_COUNT {
            let button = UIButton()
//            button.backgroundColor = randomColor()
            //设置emoji  字体大小
            button.titleLabel?.font = UIFont.systemFontOfSize(34)
            contentView.addSubview(button)
            emotionButtons.append(button)
        }
        //添加删除按钮
        contentView.addSubview(deleteButton)
        
    } 

    override func layoutSubviews() {
        super.layoutSubviews()
//        label?.center = CGPointMake(width * 0.5, height * 0.5)
        
        let margin: CGFloat = 30
        //计算每一个按钮的宽高
        let emotionButtonW = width / CGFloat(PAGE_EMOTION_MAX_COLUMN)
        let emotionButtonH = (height - margin) / CGFloat(PAGE_EMOTION_MAX_ROW)
        
        //设置每一个按钮的位置
        for (index, value) in emotionButtons.enumerate() {
            value.size = CGSizeMake(emotionButtonW, emotionButtonH)
            
            let row = index / PAGE_EMOTION_MAX_COLUMN
            let col = index % PAGE_EMOTION_MAX_COLUMN
            value.x = emotionButtonW * CGFloat(col)
            value.y = emotionButtonH * CGFloat(row)
        }
        
        
        //设置删除按钮的位置
        deleteButton.size = CGSizeMake(emotionButtonW, emotionButtonH)
        deleteButton.x = width - deleteButton.width
        deleteButton.y = height - deleteButton.height - margin
        
        //设置最近label大小
        recentLabel.centerX = width * 0.5
        recentLabel.y = height - recentLabel.height - 5
    }
    
    //最近表情的label
    private lazy var recentLabel: UILabel = {
            let label = UILabel()
        label.text = "最近使用的表情"
        label.textColor = UIColor.grayColor()
        label.font = UIFont.systemFontOfSize(12)
        label.sizeToFit()
        
        return label
    }()
    
    //删除按钮
    private lazy var deleteButton: UIButton = {
            let button = UIButton()
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        
            return button
    }()
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
